﻿// Copyright (C) 2012 Jeff Tanner <jeff00seattle@gmail.com>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
// 

using System;
using System.IO;
using System.Configuration;
using System.Diagnostics;
using System.ComponentModel;
using System.Threading;

using WindowsAzure_REST;

namespace WindowsAzure_FileBackup_Common
{
    public partial class Common
    {
        public System.IO.FileSystemWatcher FileMonitor { get; set; }
        public System.Diagnostics.EventLog EventLog { get; set; }
        public static BlobREST BlobREST { get; private set; }

        public Common()
        {
            this.initialize();
        }

        private void initialize()
        {
            this.FileMonitor = new System.IO.FileSystemWatcher();
            this.EventLog = new System.Diagnostics.EventLog();

            if (!System.Diagnostics.EventLog.SourceExists(Common.EventLogSourceAppConfig))
            {
                System.Diagnostics.EventLog.CreateEventSource(
                  Common.EventLogSourceAppConfig,
                  Common.EventLogNameAppConfig
                  );
            }

            //
            // EventLog
            //
            this.EventLog.Log = Common.EventLogNameAppConfig;
            this.EventLog.Source = Common.EventLogSourceAppConfig;
        }

        public static StorageConnectionMode WindowsAzureConnectionMode
        {
            get
            {
                return Common.WindowsAzureConnectionModeRegistry;
            }
            set
            {
                Common.WindowsAzureConnectionModeRegistry = value;
            }
        }

        public static string WindowsAzureStorageAccountName
        {
            get
            {
                if (StorageConnectionMode.Local == Common.WindowsAzureConnectionMode)
                {
                    return Common.WindowsAzureLocalStorageAccountNameAppConfig;
                }
                else
                {
                    return Common.WindowsAzureCloudStorageAccountNameRegistry;
                }
            }
            set
            {
                if (StorageConnectionMode.Cloud == Common.WindowsAzureConnectionMode)
                {
                    Common.WindowsAzureCloudStorageAccountNameRegistry = value;
                }
            }
        }

        public static string WindowsAzureStorageAccountKey
        {
            get
            {
                if (StorageConnectionMode.Local == Common.WindowsAzureConnectionMode)
                {
                    return Common.WindowsAzureLocalStorageAccountKeyAppConfig;
                }
                else
                {
                    return Common.WindowsAzureCloudStorageAccountKeyRegistry;
                }
            }
            set
            {
                if (StorageConnectionMode.Cloud == Common.WindowsAzureConnectionMode)
                {
                    Common.WindowsAzureCloudStorageAccountKeyRegistry = value;
                }
            }
        }

        public static string WindowsAzureStorageBlobContainerName
        {
            get
            {
                if (String.IsNullOrEmpty(Common.WindowsAzureStorageBlobContainerNameRegistry))
                {
                    Common.WindowsAzureStorageBlobContainerNameRegistry = Common.WindowsAzureStorageBlobContainerNameAppConfig;
                }

                return Common.WindowsAzureStorageBlobContainerNameRegistry;
            }
            set
            {
                Common.WindowsAzureStorageBlobContainerNameRegistry = value;
            }
        }

        public static string WindowsAzureStorageEndpoint
        {
            get
            {
                if (StorageConnectionMode.Local == Common.WindowsAzureConnectionMode)
                {
                    return String.Format(Common.WindowsAzureLocalStorageEndpointAppConfig, Common.WindowsAzureStorageAccountName);
                }
                else
                {
                    return String.Format(Common.WindowsAzureCloudStorageEndpointAppConfig, Common.WindowsAzureStorageAccountName);
                }
            }
        }

        public static string WindowsAzureFileBackupRootDir
        {
            get
            {
                return Common.WindowsAzureFileBackupRootDirRegistry;
            }
            set
            {
                Common.WindowsAzureFileBackupRootDirRegistry = value;
            }
        }

        public static string WindowsAzureFileRecoveryRootDir
        {
            get
            {
                return Common.WindowsAzureFileRecoveryRootDirRegistry;
            }
            set
            {
                Common.WindowsAzureFileRecoveryRootDirRegistry = value;
            }
        }

        public static void SetupBlobREST()
        {
            StorageEndpoint storageEndpoint = new StorageEndpoint
            {
                ConnectionMode = Common.WindowsAzureConnectionMode,
                Endpoint = Common.WindowsAzureStorageEndpoint,
                StorageAccount = Common.WindowsAzureStorageAccountName,
                StorageKey = Common.WindowsAzureStorageAccountKey
            };

            Common.BlobREST = new BlobREST(storageEndpoint);
        }

    }
}
